package com.xinfang.superminer.view.pop;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.xinfang.superminer.R;

/**
 * 矿机列表操作
 * Created by Administrator on 2018/3/26.
 */

public class MinerOperationPop extends PopupWindow implements View.OnClickListener {

    private View view;
    private Activity context;



    private OnSelectPos onSelectPos;


    public MinerOperationPop(Activity context,OnSelectPos onSelectPos) {
        super(context);
        this.context = context;
        this.onSelectPos=onSelectPos;
        view = LayoutInflater.from(context).inflate(R.layout.pop_operation, null);
        initView();
        initPop();

    }

    private void initView() {
        LinearLayout ll1=view.findViewById(R.id.ll1);
        ll1.setOnClickListener(this);
        LinearLayout ll2=view.findViewById(R.id.ll2);
        ll2.setOnClickListener(this);
        LinearLayout ll3=view.findViewById(R.id.ll3);
        ll3.setOnClickListener(this);
        LinearLayout ll4=view.findViewById(R.id.ll4);
        ll4.setOnClickListener(this);
        LinearLayout ll5=view.findViewById(R.id.ll5);
        ll5.setOnClickListener(this);
        LinearLayout ll6=view.findViewById(R.id.ll6);
        ll6.setOnClickListener(this);
        LinearLayout ll7=view.findViewById(R.id.ll7);
        ll7.setOnClickListener(this);
    }


    /**
     * 样式
     */
    private void initPop() {
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);


        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        this.setContentView(view);

//        setAnimationStyle(R.style.mypopwindow_anim_style);
        ColorDrawable dw = new ColorDrawable(Color.parseColor("#00000000"));
        //设置SelectPicPopupWindow弹出窗体的背景
        setBackgroundDrawable(dw);

        backgroundAlpha(1.0f);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });
        //获取popwindow焦点
        setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        setOutsideTouchable(true);
    }

    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        context.getWindow().setAttributes(lp);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll1:
                onSelectPos.getSelectPos(1);
                dismiss();
                break;
            case R.id.ll2:
                onSelectPos.getSelectPos(2);

                dismiss();
                break;
            case R.id.ll3:

                onSelectPos.getSelectPos(3);
                dismiss();
                break;
            case R.id.ll4:
                onSelectPos.getSelectPos(4);

                dismiss();
                break;
            case R.id.ll5:
                onSelectPos.getSelectPos(5);

                dismiss();
                break;
            case R.id.ll6:

                onSelectPos.getSelectPos(6);
                dismiss();
                break;
             case R.id.ll7:

                onSelectPos.getSelectPos(7);
                dismiss();
                break;

        }
    }

    /**
     * 获取点击的pos
     */
    public interface OnSelectPos{
        void getSelectPos(int pos);
    }

}
